function OUT_Bits = HF_Vitdec_Tailbiting_K7_Punct_R43(IN_Code,t)
%*----------------------------- HF_Vitdec_Tailbiting_K7_Punct_R43 ----------------------------------%
%
% OUT_Bits = HF_Vitdec_Tailbiting_K7_Punct_R43(IN_Code)
%
% Is the viterbi decoder for a convolutional code (tail biting k =7) and
% punctured with final rate (R =4/3)
% the input code is a soft values (+ve for logic '1' and -ve for logic '0').
%*--------------------------------------------------------------------------------------------------*%

%*----------------------------------- INITIALIZATIONS ----------------------------------------------*%

K = 7;
R = 3/4;

punc_vec = [1 1 1 0 0 1];  % puncturing pattern
tblen = 256;    %Trace back length
% t = poly2trellis(K, [Generator1 Generator2]); % Define trellis.
viterbiPreCursorSize = tblen ;
%*---------------------------------------------------------------------------------------------------*%

%*----------------------------------- VITERBI DECODER -----------------------------------------------*%
% IN_Code2 = [IN_Code IN_Code(1:2*tblen)]; %Adding the first two blocks at the end because ,
% we didn't know the correct start state but knowing that it is equal to
% the end state
IN_Code1 = [ IN_Code(end-viterbiPreCursorSize+1:end) IN_Code IN_Code(1:viterbiPreCursorSize)];


Decoded_Bits1 = vitdec(IN_Code1,t,tblen,'trunc','unquant',punc_vec); % Decode
% choose trunc because it starts with zero state (wrong but no problem for
% the start) and ends with the best metric state.

% Decoded_Bits = vitdec(IN_Code2,t,tblen,'trunc','unquant',punc_vec); % Decode

% choose unquant, you must enter the data in a polar format as described
% above.

% Decoded_Bits(1:tblen*R) = Decoded_Bits(end-tblen*R*2+1:end-tblen*R);
% discard the first block and replace it with last correct one.

% MSG = Decoded_Bits(1:end-tblen*2*R);
% discard the last block (we replace it already with the first one)

% OUT_Bits1 = [MSG(end-(K-1)+1:end) MSG(1:end-(K-1))]; % format the MSG from the tail biting encoding form.
%*--------------------------------------------------------------------------------------------------*%
MSG = Decoded_Bits1(viterbiPreCursorSize*R+1:viterbiPreCursorSize*R+length(IN_Code)*R);
OUT_Bits = [MSG(end-(K-1)+1:end) MSG(1:end-(K-1))];
